""" Output mode to display boat summary """
from pygame.locals import *

from pgEngine.point import Point

from colors import *

class ShowBoatMode:
    def __init__(self, game, window, ship):
        self.game = game
        self.window = window
        self.map = window.map
        
        # Set the source
        self.boat =  ship
        self.origin = self.game.ocean.getCoord(self.boat) + Point(0.5, -0.5)
        self.boatPos = self.map.getScreenPos(self.origin)

        # Load the overlay.  TODO: Move this somewheres else.
        self.parchment = self.map.res.loadImage("parchment.png")
        self.parchment.set_alpha(230)
        self.highlight = self.map.res.loadImage("boatHighlight.png")
        self.upgrades = self.map.res.loadImage("buttons.png")
        self.anchor = self.map.res.loadImage("anchor.png")
        #self.upgrades =  self.map.res.renderDialog(100,40)

        titleFont = self.map.res.loadFont("Michael.ttf", 20)
        descFont = self.map.res.loadFont("Chanticleer.ttf", 14)

        if len(self.boat.name) > 25:
            title = self.boat.name[:22]+"..."
        else:
            title = self.boat.name
        self.title = titleFont.render(title, 1, (0,0,0))
        nCrew = len(self.boat.crew) or "No"
        nGuns = self.boat.curCannon or "No"
        if nGuns != 1:
            plural = "Guns"
        else:
            plural = "Gun"
        self.description = descFont.render("%s Crew, %s %s" %
                                                (nCrew, nGuns, plural),
                                           1, (50,50,50))
        self.pSize = Point(*self.parchment.get_size())
        self.showPos = self.boatPos + Point(-self.pSize.x/2, -self.pSize.y*5/3)
        self.uSize = Point(*self.upgrades.get_size())
        self.upgPos = self.boatPos + Point(-self.uSize.x/2 + 4, self.uSize.y)

        tSize = Point(*self.title.get_size())
        tOffset = (self.pSize - tSize) / 2 + Point(0.0, -self.pSize.y/6)

        dSize = Point(*self.description.get_size())
        dOffset = (self.pSize - dSize) / 2 + Point(0.0, tSize.y / 2)
        self.tOffset = tOffset
        self.dOffset = dOffset
    
    def onEvent(self, event):
        if (event.type == MOUSEBUTTONDOWN and
            event.button == 2):
            self.boat.anchored = not self.boat.anchored
            if self.boat.anchored:
                self.boat.finishTurn()
            else:
                self.boat.startTurn()
            return False
        elif event.type == MOUSEMOTION:
            return False
            
    
    def onDrawMapTiles(self, surface):
        hSize = self.highlight.get_size()
        highlightLoc = (self.boatPos.x - hSize[0] / 2,
                        self.boatPos.y - hSize[1] / 2)
        surface.blit(self.highlight, highlightLoc)
    
    def onDrawMapObjects(self, surface):        
        wndMap = self.window.map
        mouseX, mouseY = self.window.mouseX, self.window.mouseY
        highlightLoc = (mouseX - wndMap.rcMap.left - wndMap.gridSize.x / 2,
                        mouseY - wndMap.rcMap.top - wndMap.gridSize.y / 2)
        surface.blit(wndMap.highlight, highlightLoc)

        surface.blit(self.parchment, self.showPos.pos)
        surface.blit(self.title, (self.showPos + self.tOffset).pos)
        surface.blit(self.description, (self.showPos + self.dOffset).pos)
        
        if self.boat.anchored:
            surface.blit(self.upgrades, self.upgPos.pos)
            surface.blit(self.anchor, self.upgPos.pos)

        # Make sure we're not hanging around.        
        mapPos = Point(*wndMap.getMousePos())
        if (mapPos - self.origin).length > 2.0:
            self.window.mode.remove(self)
